const express = require('express')
const sequelize = require('./config/database')
const router = require('./router/router')
const auth = require('./middleware/auth.js')
const cors = require('cors')
const requestType = require('./middleware/requestType.js')

const app = express()

app.use(express.json())
app.use(express.urlencoded({ extended: true }))

// 连接同步数据库
sequelize
  .sync()
  .then(() => {
    // console.log('同步数据库成功')
  })
  .catch((err) => {
    console.error('数据库同步失败:', err)
  })

// 允许跨域中间件
app.use(cors())

// 其他中间件
app.use(auth)

// 获取http请求类型
app.use(requestType)

// 挂载路由中间件
app.use('/api', router)

// 启动服务器
app.listen(3000, () => {
  console.log(`Server is running on port 3000`)
})
